package cn.rongdao.jrkabao.ui.remind;

import android.app.Activity;

import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import cn.rongdao.jrkabao.bean.remind.RemindAmountQueryBean;
import cn.rongdao.jrkabao.model.remind.RemindAmountQueryModel;
import cn.rongdao.jrkabao.model.remind.RemindChangeRepayStatusModel;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.widget.SelectPaymentWayFromBottom;

/**
 * Created by jack on 2017/12/5
 */

public class SelectFromBottomManager {

    private SelectPaymentWayFromBottom selectPaymentWayFromBottom;
    private SelectPaymentWayFromBottom selectCheckBalanceWayFromBottom;
    private Activity activity;
    private OnChangeRepayStatusListener onChangeRepayStatusListener;

    public SelectFromBottomManager(Activity activity) {
        this.activity = activity;
        selectPaymentWayFromBottom = new SelectPaymentWayFromBottom(activity, true);
        selectCheckBalanceWayFromBottom = new SelectPaymentWayFromBottom(activity, false);
    }

    public void setOnCheckBalanceClickListener(SelectPaymentWayFromBottom.OnCheckBalanceClickListener onCheckBalanceClickListener) {
        selectCheckBalanceWayFromBottom.setOnCheckBalanceClickListener(onCheckBalanceClickListener);
    }

    public void setOnSelectListener(SelectPaymentWayFromBottom.OnSelectListener onSelectListener) {
        selectPaymentWayFromBottom.setOnSelectListener(onSelectListener);
    }

    public void remindAmountQuery(BaseImpl baseImpl, String remindId) {
        RemindAmountQueryModel.getInstance().execute(remindId, new FRBaseObserver<RemindAmountQueryBean>(baseImpl) {
            @Override
            protected void onBaseNext(RemindAmountQueryBean data) {
                sendMsg(data.getPhone(), data.getContent());
                dismissPaymentPop();
            }

            @Override
            protected void onBaseError(Throwable t) {
                FRToast.showToastSafe(FRString.valueOf(t.getMessage()));
            }
        });
    }

    public void remindChangeRepayStatus(BaseImpl baseImpl, String remindId, boolean isSelected) {
        RemindChangeRepayStatusModel.getInstance().execute(remindId, isSelected, new FRBaseObserver<String>(baseImpl, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(String data) {
                if (null != onChangeRepayStatusListener) {
                    onChangeRepayStatusListener.onChangeRepayStatusSuccess();
                }
                dismissPaymentPop();
            }

            @Override
            protected void onBaseError(Throwable t) {
                FRToast.showToastSafe(FRString.valueOf(t.getMessage()));
            }
        });
    }

    public void dismissPaymentPop() {
        selectPaymentWayFromBottom.dismiss();
    }

    public void dismissCheckBalancePop() {
        selectCheckBalanceWayFromBottom.dismiss();
    }

    private void sendMsg(String phone, String content) {
        activity.startActivity(CommonUtil.getMsgIntent(phone, content));
    }

    public void showPaymentPop(String buttonStatus) {
        selectPaymentWayFromBottom.setRepaymentIsSelect(CommonUtil.isStatusSuccess(buttonStatus));
        selectPaymentWayFromBottom.show();
    }

    public void showCheckBalancePop(String isExport) {
        if (!CommonUtil.isStatusSuccess(isExport)) {
            selectCheckBalanceWayFromBottom.show();
        }
    }

    public void setOnChangeRepayStatusListener(OnChangeRepayStatusListener onChangeRepayStatusListener) {
        this.onChangeRepayStatusListener = onChangeRepayStatusListener;
    }

    public interface OnChangeRepayStatusListener {
        void onChangeRepayStatusSuccess();
    }
}
